package leetcode_500;

/**
 *@author 周杨
 *IslandPerimeter_463 计算一个岛的周长
 *describe:遍历即可  AC 8%
 *2018年8月28日 上午11:09:29
 */
public class IslandPerimeter_463 {
	 public int islandPerimeter(int[][] grid) {
	     if(grid.length==0||grid[0].length==0)
	    	 return 0;
	     int res=0;
	     int row=grid.length,col=grid[0].length;
	     for(int i=0;i<row;++i) {
	    	 if(grid[i][0]==1)
	    		 ++res;
	    	 if(grid[i][col-1]==1)
	    		 ++res;
	     }
	     for(int i=0;i<col;++i) {
	    	 if(grid[0][i]==1)
	    		 ++res;
	    	 if(grid[row-1][i]==1)
	    		 ++res;
	     }
	     for(int i=0;i<row-1;++i)
	    	 for(int j=0;j<col-1;++j) {
	    		if(grid[i][j]!=grid[i+1][j])
	    			++res;
	    		if(grid[i][j]!=grid[i][j+1])
	    			++res;
	    	 }
	     for(int i=0;i<col-1;++i) {
	    	 if(grid[row-1][i]!=grid[row-1][i+1])
	    		 ++res;
	     }
	     for(int i=0;i<row-1;++i)
	    	 if(grid[i][col-1]!=grid[i+1][col-1])
	    		 ++res;
	     return res;
	 }
}
